package com.fwlst.module_home.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.alibaba.android.arouter.launcher.ARouter
import com.fwlst.lib_base.constant.AppConfig
import com.fwlst.lib_base.constant.GlobalUserInfo
import com.fwlst.lib_base.constant.SettingRoute
import com.fwlst.lib_base.constant.UserRoute
import com.fwlst.lib_base.model.UserViewModel
import com.fwlst.lib_base.ui.BaseListItem
import com.fwlst.module_home.R
import com.hjq.toast.Toaster
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Composable
fun ProfileScreen() {
    val showClearCacheDialog = remember { mutableStateOf(false) }
    val isLoading = remember { mutableStateOf(false) }
    val userViewModel: UserViewModel = viewModel()

    val isLogIn by GlobalUserInfo.isLogIn.collectAsState()
    val displayUserName by userViewModel.displayUserName.collectAsState()



    fun goToSetting(pageName: String) {
        ARouter.getInstance().build("/base/route/SettingActivity")
            .withString("pageName", pageName)
            .navigation()
    }

    fun goToUser(pageName: String) {
        ARouter.getInstance().build("/base/route/UserActivity")
            .withString("pageName", pageName)
            .navigation()
    }

    // 背景图
    Image(
        painter = painterResource(id = R.drawable.home_profile_bg),
        contentDescription = "背景图",
        contentScale = ContentScale.FillBounds,
        modifier = Modifier.fillMaxSize()
    )
    Column {
        Column {
            if (AppConfig.PAYMODEL && !AppConfig.IS_OFFLINE) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 80.dp, bottom = 30.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Column(
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(150.dp)
                                .clickable {
                                    if (isLogIn) {
                                        goToUser(UserRoute.USER_INFO)
                                    } else {
                                        goToUser(UserRoute.USER_LOGIN)
                                    }
                                },
                            contentAlignment = Alignment.Center
                        ) {
                            Column(
                                horizontalAlignment = Alignment.CenterHorizontally
                            ) {
                                Image(
                                    modifier = Modifier
                                        .size(100.dp)
                                        .padding(bottom = 15.dp),
                                    painter = painterResource(id = R.drawable.home_def_avatar),
                                    contentDescription = null
                                )
                                Text(
                                    text = displayUserName,
                                    fontSize = 16.sp,
                                    color = Color(0xFF010101),
                                    fontWeight = FontWeight.Bold,
                                )
                            }
                        }
                        Image(
                            modifier = Modifier
                                .padding(top = 30.dp)
                                .fillMaxWidth()
                                .clickable {
                                    ARouter
                                        .getInstance()
                                        .build("/home/route/MemberCenterActivity")
                                        .navigation()
                                },
                            painter = painterResource(id = R.drawable.home_vip_banner),
                            contentScale = ContentScale.Fit,
                            contentDescription = null
                        )
                    }
                }
            } else {
                Text(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 25.dp, vertical = 70.dp),
                    text = "个人中心",
                    fontSize = 40.sp,
                    color = Color(0xFF010101),
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center
                )
            }
        }
        LazyColumn(
            modifier = Modifier
                .fillMaxSize().background(Color.White)
        ) {
            item {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(Color.White)
                ) {
                    BaseListItem(
                        text = "隐私设置",
                        leftIcon = com.fwlst.lib_base.R.drawable.base_privacy,
                        clickable = {
                            goToSetting(SettingRoute.PRIVACY_PARENT)
                        }
                    )
                    HorizontalDivider()
                    BaseListItem(
                        text = "投诉举报",
                        leftIcon = com.fwlst.lib_base.R.drawable.base_complaint,
                        clickable = {
                            goToSetting(SettingRoute.COMPLAINT_PARENT)
                        }
                    )
                    HorizontalDivider()
                    if(!AppConfig.IS_OFFLINE){
                        BaseListItem(
                            text = "联系客服",
                            desc = "工作日：09:00-18:00",
                            leftIcon = com.fwlst.lib_base.R.drawable.base_service,
                            clickable = {
                                ARouter.getInstance().build("/base/route/WebViewActivity")
                                    .withString("url", AppConfig.USER_SERVICE_URL)
                                    .navigation()
                            }
                        )
                        HorizontalDivider()
                    }
                    BaseListItem(
                        text = "清除缓存",
                        leftIcon = R.drawable.home_profile_cache,
                        clickable = {
                            // 添加弹窗状态控制
                            showClearCacheDialog.value = true
                        }
                    )
                    HorizontalDivider()
                    BaseListItem(
                        text = "关于我们",
                        leftIcon = R.drawable.home_profile_about
                    ) {
                        goToSetting(SettingRoute.ABOUT_US)
                    }
                }
            }
        }
    }


    if (showClearCacheDialog.value) {
        AlertDialog(
            onDismissRequest = { showClearCacheDialog.value = false },
            title = { Text("确认清除缓存？") },
            confirmButton = {
                TextButton(
                    onClick = {
                        isLoading.value = true
                        showClearCacheDialog.value = false

                        // 模拟清除缓存操作
                        CoroutineScope(Dispatchers.Main).launch {
                            delay(3000)
                            isLoading.value = false
                            Toaster.show("清除成功")
                        }
                    }
                ) { Text("确定") }
            },
            dismissButton = {
                TextButton(
                    onClick = { showClearCacheDialog.value = false }
                ) { Text("取消") }
            }
        )
    }

    if (isLoading.value) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.Black.copy(alpha = 0.5f)),
            contentAlignment = Alignment.Center
        ) {
            CircularProgressIndicator(color = Color.White)
        }
    }
}